package com.study.mike.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.study.mike.model.po.DeleteBatchPO;
import com.study.mike.model.po.SaveOrUpdateContractPO;
import com.study.mike.model.query.ContractQuery;
import com.study.mike.result.R;
import com.study.mike.result.RUtil;
import com.study.mike.service.ContractService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author KangHui
 * @Date 2023/9/20 16:24
 */
@RestController()
@RequestMapping("contract")
public class ContractController {
    @Resource
    private ContractService contractService;

    /**
     * saveOrUpdateContract
     *
     * @param po
     * @return
     */
    @PostMapping("/saveOrUpdate")
    public R saveOrUpdate(@RequestBody SaveOrUpdateContractPO po) {
        if (StrUtil.isBlank(po.getFlowNo())) {
            return RUtil.fail("流程编号不能为空");
        }
        return RUtil.success(contractService.saveOrUpdate(po));
    }

    /**
     * queryContract
     *
     * @param po
     * @return
     */
    @PostMapping("/query")
    public R queryContract(@RequestBody ContractQuery po) {
        return RUtil.success(contractService.queryContract(po));
    }

    /**
     * info
     *
     * @param po
     * @return
     */
    @PostMapping("/info")
    public R contractInfo(@RequestBody ContractQuery po) {
        if (ObjectUtil.isNull(po.getId())) {
            return RUtil.fail("请选择查询数据");
        }
        return RUtil.success(contractService.contractInfo(po));
    }

    /**
     * deleteBatch
     *
     * @param po
     * @return
     */
    @PostMapping("/deleteBatch")
    public R deleteBatch(@RequestBody DeleteBatchPO po) {
        if (CollUtil.isEmpty(po.getIds())) {
            return RUtil.fail("请选择待删数据");
        }
        Boolean flag = contractService.deleteBatch(po);
        if (flag) {
            return RUtil.success(true);
        }
        return RUtil.fail();
    }

    /**
     * export
     *
     * @param po
     * @return
     */
    @PostMapping("/export")
    public R export(@RequestBody ContractQuery po) {
        contractService.export(po);
        return RUtil.success();
    }

    /**
     * import
     */
    @PostMapping("/import")
    public R importContract() {
        return RUtil.success();
    }

}
